package org.jdsm.gae.komarichyn.contact;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.LocalizedTextUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.jdsm.flow.contact.IContactEntity;
import org.jdsm.flow.contact.IContactFeedBack;
import org.jdsm.flow.contact.IContactService;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * User: mikom
 * Date: 2/8/13
 * Time: 4:58 PM
 */
public class ContactAction extends ActionSupport {

    @Resource
    private IContactService contactService;
    @Resource(name = "feedBack")
    private IContactFeedBack feedBack;
    @Resource(name = "contact")
    private IContactEntity contact;

    private String upload;
    private String uploadContentType;
    private int uploadContentLength;
    private InputStream uploadStream;

    private List<IContactEntity> contacts = new ArrayList<IContactEntity>();


    @Override
    public String execute() throws Exception {
        contacts = contactService.getContacts();
        return SUCCESS;
    }

    public String sendFeedBack() throws Exception {
        feedBack.setEmail("Michael.Komarichin@gmail.com");
        contactService.saveFeedBack(feedBack);
        return SUCCESS;
    }

    public String addContact() throws Exception {
        uploadStream = IOUtils.toInputStream(upload, "ISO-8859-1");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int read;
        while ((read = uploadStream.read()) != -1) {
            baos.write(read);
        }

        contact.setImg("data:"+ uploadContentType +";base64," + Base64.encodeBase64String(baos.toByteArray()));
        contact.setLink(MessageFormat.format(contact.getLink(), contact.getValue()));

        contactService.addContact(contact);

        return SUCCESS;
    }

    public String removeContact() throws Exception{
        contactService.removeContact(contact);
        return SUCCESS;
    }

    /*public String view() throws Exception{
        contact = contactService.loadContact(contact);
        uploadStream = new ByteArrayInputStream(contact.getImg());
        uploadContentType = "image/png";
        uploadContentLength = contact.getImg().length;
        return SUCCESS;
    }*/

    public IContactFeedBack getFeedBack() {
        return feedBack;
    }

    public void setFeedBack(IContactFeedBack feedBack) {
        this.feedBack = feedBack;
    }

    public IContactEntity getContact() {
        return contact;
    }

    public void setContact(IContactEntity contact) {
        this.contact = contact;
    }

    public String getUpload() {
        return upload;
    }

    public void setUpload(String upload) {
        this.upload = upload;
    }

    public String getUploadContentType() {
        return uploadContentType;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }


    public InputStream getUploadStream() {
        return uploadStream;
    }

    public void setUploadStream(InputStream uploadStream) {
        this.uploadStream = uploadStream;
    }

    public List<IContactEntity> getContacts() {
        return contacts;
    }

    public void setContacts(List<IContactEntity> contacts) {
        this.contacts = contacts;
    }

    public int getUploadContentLength() {
        return uploadContentLength;
    }

    public void setUploadContentLength(int uploadContentLength) {
        this.uploadContentLength = uploadContentLength;
    }
}
